package com.taotao.controller;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class QRCode {

    public static void main(String[] args) {
        // 1、递归扫描一个文件夹下的所有图片文件
        String path="C:\\Users\\DELL\\Pictures\\4bb3ec67-0a96-4f65-9dc7-9822259d74ed.jpg";
//        String path="C:\\Users\\DELL\\Pictures\\t0192caa673e76c4e70.png";
        QRCode qrCodeFound=new QRCode();
        String content=qrCodeFound.checkQRCode(path);
        System.out.println("content-->"+content);

    }

    private String checkQRCode(String path) {
        String content=null;
        BufferedImage image;
        try{
            image= ImageIO.read(new File(path));
            LuminanceSource source=new BufferedImageLuminanceSource(image);
            Binarizer binarizer=new HybridBinarizer(source);
            BinaryBitmap binaryBitmap=new BinaryBitmap(binarizer);
            Map<DecodeHintType,Object> hints=new HashMap<>();
            hints.put(DecodeHintType.CHARACTER_SET,"UTF-8");
            Result result=new MultiFormatReader().decode(binaryBitmap,hints);
            System.out.println("图片中的内容："+result.getText());
            content=result.getText();
        }catch (IOException e) {
            e.printStackTrace();
        }catch (NotFoundException e){
            e.printStackTrace();
        }
        return content;
    }
}
